package aima.core.environment.eightpuzzle;

import aima.core.search.framework.HeuristicFunction;
import aima.core.util.datastructure.XYLocation;

/**
 * Heuristica de la tarea
 * @author Oscar Villate
 * 		   Andres Peralta
 */
public class Heuristica8PuzzleTrabajoEnClase2 implements HeuristicFunction
{
	/**
	 * Funciona de estado
	 */
	public double h(Object state) 
	{
		EightPuzzleBoard tablero = (EightPuzzleBoard) state;
		return evaluateWhoIsMissing(tablero);
	}

	/**
	 * Evalua cuales numeros faltan por posicionar
	 */
	private int evaluateWhoIsMissing(EightPuzzleBoard tablero) 
	{
		int numberWhoIsMissing = 0;
		
		if (!(tablero.getLocationOf(0).equals(new XYLocation(0, 0)))) {
			numberWhoIsMissing += 0;
		}
		if (!(tablero.getLocationOf(1).equals(new XYLocation(0, 1)))) {
			numberWhoIsMissing += 1;
		}
		if (!(tablero.getLocationOf(2).equals(new XYLocation(0, 2)))) {
			numberWhoIsMissing += 2;
		}
		if (!(tablero.getLocationOf(3).equals(new XYLocation(1, 0)))) {
			numberWhoIsMissing += 3;
		}
		if (!(tablero.getLocationOf(4).equals(new XYLocation(1, 1)))) {
			numberWhoIsMissing += 4;
		}
		if (!(tablero.getLocationOf(5).equals(new XYLocation(1, 2)))) {
			numberWhoIsMissing += 5;
		}
		if (!(tablero.getLocationOf(6).equals(new XYLocation(2, 0)))) {
			numberWhoIsMissing += 6;
		}
		if (!(tablero.getLocationOf(7).equals(new XYLocation(2, 1)))) {
			numberWhoIsMissing += 7;
		}
		if (!(tablero.getLocationOf(8).equals(new XYLocation(2, 2)))) {
			numberWhoIsMissing += 8;
		}
		return numberWhoIsMissing;
	}
}